package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.itcast.commons.CommonUtils;
import dao.UserDAO;
import dao.impl.UserDAOImpl;
import entity.School;
import entity.User;
import service.SchoolService;
import service.UserService;
import service.impl.SchoolServiceImpl;
import service.impl.UserServiceImpl;

/**
 * Servlet implementation class RegistServlet
 */
@WebServlet("/RegistServlet")
public class RegistServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RegistServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		SchoolService schoolService = new SchoolServiceImpl();
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
//		User form = CommonUtils.toBean(request.getParameterMap(), User.class);

		// 读取参数
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String sname = request.getParameter("school");
		String register_gender = request.getParameter("gender");
		String register_phone = request.getParameter("phone");
		String register_email = request.getParameter("email");
		String register_studentid = request.getParameter("studentid");
		String register_realname = request.getParameter("realname");
		String verifyCode = request.getParameter("verifyCode");
		School school = schoolService.findBySname(sname);
		UserDAO userDAO ;
		userDAO=new UserDAOImpl();
		User user = new User(0, school, username, password, register_gender, register_phone, register_email,
				register_realname, register_studentid, 10, 10, 0,"",0,0);
		int i = userDAO.addUser(user);
		String sessionVerifyCode = (String) request.getSession().getAttribute("session_vcode");
		if (i==1 && !verifyCode.equalsIgnoreCase(sessionVerifyCode)) {
			response.getWriter().print(
					"<h1>提交注册成功！进行审核！</h1><a href='" + request.getContextPath() + "/user/index.jsp" + "'>返回首页</a>");
		} else {
			response.getWriter().print(
					"<h1>提交失败TAT！！</h1><a href='" + request.getContextPath() + "/user/register.jsp" + "'>返回注册</a>");
		}

		/**
		 * 添加表单校验任务 1.创建一个Map，用来装载所有的表单信息 在校验过程中，如果失败，向Map添加错误信息，其中key为表单字段名称
		 * 2.校验之后，查看Map长度是否大于0，如果大于0，说明有错误信息，就是有错误，
		 * 将Map保存到requsest中，保存form到request中，转发到regist.jsp 
		 * 3.如果Map为空，说明没有错误，向下执行代码
		 */

	}

}
